package cn.zxl.graduate.service.impl;

import cn.zxl.graduate.domain.User;
import cn.zxl.graduate.exception.BusinessException;
import cn.zxl.graduate.mapper.UserMapper;
import cn.zxl.graduate.myenum.SetKeyInRedis;
import cn.zxl.graduate.service.IRedisService;
import cn.zxl.graduate.service.IUserService;
import cn.zxl.graduate.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bo-feifei
 * @since 2021-11-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IRedisService redisService;

    @Override
    public boolean isExistForUserByUserName(String username) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username",username);
        User user = userMapper.selectOne(query);
        return user != null;
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        AssertUtil.hasText(username,"账号不能为空");
        AssertUtil.hasText(password,"密码不能为空");

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        User user = getOne(wrapper);

        if (user == null) {
            throw new BusinessException("账号密码输入错误，请重新输入");
        }

        String token = UUID.randomUUID().toString().replace("-", "");
        String userStr = JSON.toJSONString(user);
        redisService.setKeyInRedis(SetKeyInRedis.USERKEY(token),userStr);

        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);

        return map;
    }

    @Override
    public void registUser(String username,
                           String password,
                           String rpassword,
                           String nickname,
                           String phone, String email) {

        AssertUtil.hasText(username,"用户名不能为空");
        AssertUtil.hasText(password,"密码不能为空");
        AssertUtil.hasText(rpassword,"重复密码不能为空");
        AssertUtil.hasText(phone,"手机号不能为空");

        boolean isHave = isExistForUserByUserName(username);
        if (isHave) {
            throw new BusinessException("该用户已存在");
        }

        if (!password.equals(rpassword)) {
            throw new BusinessException("两次输入的密码不相等");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        user.setPhone(phone);
        user.setEmail(email);
        userMapper.insert(user);


    }

}
